<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="查询类型" prop="wtype">
        <el-select
          v-model="queryParams.wtype"
          placeholder="请选择查询类型"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.wtype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="withdrawalList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        :label="selectDictLabel(dict.type.wtype, queryParams.wtype) + '名称'"
        align="center"
        prop="userName"
        min-width="150"
      />
      <el-table-column
        label="提现金额"
        align="center"
        prop="applyAmount"
        width="120"
      />
      <el-table-column
        label="提现手续费"
        align="center"
        prop="estimatedAmount"
        width="120"
      />
      <el-table-column
        label="应付金额"
        align="center"
        prop="amount"
        width="100"
      />
      <!-- <el-table-column label="实际到账金额" align="center" prop="actualAmount" /> -->
      <el-table-column
        label="提现方式"
        align="center"
        prop="payMethod"
        width="100"
      >
        <template slot-scope="{ row }">
          <dict-tag :options="dict.type.pay_method" :value="row.payMethod" />
        </template>
      </el-table-column>
      <el-table-column label="收款人姓名" align="center" prop="payeeName" />
      <el-table-column
        label="银行卡号"
        align="center"
        prop="bankCard"
        min-width="200"
      />
      <el-table-column
        label="开户行"
        align="center"
        prop="bankInfo"
        min-width="200"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="applyStatus"
        width="100"
      >
        <template slot-scope="{ row }">
          <dict-tag :options="dict.type.withdrawal_status" :value="row.applyStatus" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="提现状态" align="center" prop="withdrawalStatus" /> -->
      <el-table-column
        label="备注"
        align="center"
        prop="descriptions"
        min-width="200"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核时间"
        align="center"
        prop="reviewTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180px"
        fixed="right"
      >
        <template slot-scope="scope" v-if="scope.row.applyStatus == 0">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdates(scope.row)"
            v-hasPermi="['finance:withdrawal:ok']"
            >审核通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:withdrawal:no']"
            >审核不通过</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改提现记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="80px">
        <el-form-item label="原因描述" prop="apText" required>
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入原因描述"
            v-model="form.apText"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWithdrawal,
  getWithdrawal,
  delWithdrawal,
  addWithdrawal,
  updateWithdrawal,
} from "@/api/order/withdrawal";

export default {
  name: "Withdrawal",
  dicts: ["wtype", "pay_method", "withdrawal_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现记录表格数据
      withdrawalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wtype: "1",
        userName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询提现记录列表 */
    getList() {
      this.loading = true;
      listWithdrawal(this.queryParams).then((response) => {
        this.withdrawalList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        apStatus: "0",
        apText: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWithdrawal(id).then((response) => {
        this.form.id = response.data.id;
        this.open = true;
        this.title = "审核不通过原因";
      });
    },
    /** 修改按钮操作 */
    handleUpdates(row) {
      this.reset();
      const id = row.id || this.ids;
      getWithdrawal(id).then((response) => {
        this.form.id = response.data.id;
      });
      this.$confirm("确定要审核通过吗?", "审核确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.form.id != null) {
          this.form.apStatus = "1";
          updateWithdrawal(this.form).then((response) => {
            this.$modal.msgSuccess("审核成功");
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.apStatus = "2";
            updateWithdrawal(this.form).then((response) => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除提现记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWithdrawal(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
